<template>
  <div class="RegisterPage">
    <header class="createAccount">创建账号</header>
    <div class="content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="" prop="creditCode">
          <el-input
            v-model="ruleForm.creditCode"
            placeholder="统一社会信用编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="supplier">
          <el-input
            v-model="ruleForm.supplier"
            placeholder="供应商名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="userName">
          <el-input
            v-model="ruleForm.userName"
            placeholder="用户姓名（请填写真实姓名）"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="phoneNumber">
          <el-input
            v-model="ruleForm.phoneNumber"
            placeholder="手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="code">
          <el-input
            class="code"
            v-model="ruleForm.code"
            placeholder="短信验证码"
          ></el-input>
          <el-button
            v-if="!isGetCode"
            class="getCode"
            type="info"
            plain
            @click="getPhoneCode"
            >免费获取验证码</el-button
          >
          <span class="countdown" v-else>{{ countdown }}s后重新获取</span>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            :type="inputType"
            v-model="ruleForm.password"
            placeholder="设置密码"
            @input="inputType='password'"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="confirmPassword">
          <el-input
            :type="inputType2"
            v-model="ruleForm.confirmPassword"
            placeholder="确认密码"
            @input="inputType2='password'"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item>
          <el-checkbox v-model="checked" class="agreementChecked">
            <span class="agreement">我已阅读并同意</span>
            <span class="blue agreement">《服务条款》</span>
            <span class="agreement">和</span>
            <span class="blue agreement">《隐私权政策》</span>
          </el-checkbox>
        </el-form-item> -->
        <div class="btns">
          <p class="goLogin" @click="goLogin()">返回首页</p>
          <p class="registerBtn" @click="registerEvt('ruleForm')">立即注册</p>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getCode, register } from "@/apis/common.js";
export default {
  data() {
    return {
      inputType: "text",//输入框类型
      inputType2: "text",//输入框类型
      loginName: "",
      password: "",
      ruleForm: {
        creditCode: "", //统一社会信用编码
        supplier: "", //供应商名称
        userName: "", //用户名称
        phoneNumber: "", //手机号码
        code: "", //短信验证码
        password: "", //设置密码
        confirmPassword: "", //确认密码
      },
      rules: {
        creditCode: [
          {
            required: true,
            message: "请输入统一社会信用编码",
            trigger: "blur",
          },
        ],
        supplier: [
          { required: true, message: "请输入供应商名称", trigger: "blur" },
        ],
        userName: [
          { required: true, message: "请输入用户姓名", trigger: "blur" },
        ],
        phoneNumber: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
        ],
        code: [
          { required: true, message: "请输入短信验证码", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入设置密码", trigger: "blur" },
        ],
        confirmPassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
        ],
      },
      isGetCode: false, //是否以获取验证码
      countdown: 0, //重新获取验证码倒计时
    };
  },
  mounted() {
    document.title = "安宁股份供应商注册";
  },
  methods: {
    // 获取验证码
    async getPhoneCode() {
      console.log(this.ruleForm.phoneNumber);
      if (this.ruleForm.phoneNumber) {
        if (this.validatePhoneNumber(this.ruleForm.phoneNumber)) {
          await getCode({ phoneNumber: this.ruleForm.phoneNumber }).then(
            (res) => {
              if (res.code == 200) {
                this.$message.success("验证码已发送，请注意查收！");
                this.isGetCode = true;
                this.countdown = 180;
                var timer = setInterval(() => {
                  this.countdown--;
                  if (this.countdown <= 0) {
                    clearInterval(timer);
                    this.isGetCode = false;
                  }
                }, 1000);
              }
            }
          );
        } else {
          this.$message.error("手机号格式不正确！");
        }
      } else {
        this.$message.error("请输入手机号");
      }
    },
    // 立即注册
    async registerEvt(formName) {
      console.log('注册参数：',this.ruleForm);
      var success = 0;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          success = 1;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      // else{
      //   success = 1;
      // }
      if (success) {
        if(this.ruleForm.password != this.ruleForm.confirmPassword){
          this.$message.error('输入的密码不一致！');
          return false;
        }
        var result = this.checkStrength(this.ruleForm.confirmPassword);
        console.log('密码强度：',result);
        if(result == 0){
          this.$message.error('密码强度太弱');
          return false;
        }
        const obj = {
          deptCode: this.ruleForm.creditCode,
          deptName: this.ruleForm.supplier,
          name: this.ruleForm.userName,
          phoneNumber: this.ruleForm.phoneNumber,
          verifyCode: this.ruleForm.code,
          password: this.ruleForm.password,
          password2: this.ruleForm.confirmPassword,
        };
        await register(obj).then((res) => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success("注册成功！");
            this.$router.push("/login");
          }
          else{
            this.$message.error(res.message);
          }
        });
      }
    },
    // 密码强度校验
    checkStrength(val) {
      var strength = 0;

      // 密码长度校验：至少 8 个字符
      if (val.length >= 8) {
        strength += 1;
      }
      
      // 检查是否包含大写字母
      if (/[A-Z]/.test(val)) {
        strength += 1;
      }
      
      // 检查是否包含小写字母
      if (/[a-z]/.test(val)) {
        strength += 1;
      }
      
      // 检查是否包含数字
      if (/\d/.test(val)) {
        strength += 1;
      }
      
      // 检查是否包含特殊字符
      if (/[!@#$%^&*(),.?":{}|<>]/.test(val)) {
        strength += 1;
      }

      // 检查是否包含常见的字典密码或简单密码（例如123456）
      var commonPasswords = ['123456', 'password', 'qwerty', 'abc123', 'letmein'];
      if (commonPasswords.includes(val)) {
        strength = 0; // 如果是常见密码，设置为最弱
      }
      
      // 根据强度设置不同的提示信息
      if (strength < 2) {
        return 0;
      } else if (strength < 4) {
        return 1;
      } else {
        return 2;
      }
    },
    // 验证手机号
    validatePhoneNumber(loginName) {
      const regex = /^1[3-9]\d{9}$/;
      return regex.test(loginName);
    },
    // 返回首页
    goLogin() {
      this.$router.push("/login");
    },
  },
};
</script>
<style lang="less" scoped>
.RegisterPage {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  .createAccount {
    width: 100%;
    height: 40px;
    line-height: 40px;
    font-size: 24px;
    text-align: center;
    margin-bottom: 20px;
  }
  .content {
    width: 320px;
    height: 400px;
    margin: 0 auto;
    /deep/.el-form-item__content {
      width: 100%;
      margin-left: 0px !important;
      .el-input__inner {
        background: rgba(193, 210, 255, 0.4);
        color: rgba(7, 40, 185, 1);
      }
    }
    .code {
      width: 160px;
      display: inline-block;
    }
    .getCode {
      width: 145px;
      margin-left: 15px;
    }
    .countdown {
      width: 145px;
      margin-left: 15px;
      display: inline-block;
      text-align: center;
      background-color: rgb(13, 207, 23);
      border: 1px solid rgb(13, 207, 23);
      box-sizing: border-box;
      border-radius: 4px;
      line-height: 38px;
    }
    .agreementChecked {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      .agreement {
        font-size: 12px;
      }
      .blue {
        color: #3562fe;
        cursor: pointer;
      }
    }
    .btns {
      width: 100%;
      display: flex;
      justify-content: space-between;
      .goLogin {
        width: 130px;
        height: 46px;
        line-height: 46px;
        text-align: center;
        background: #97979b;
        color: rgba(255, 255, 255, 1);
        font-size: 16px;
        border: 1px solid #97979b;
        border-radius: 4px;
        cursor: pointer;
      }
      .registerBtn {
        width: 130px;
        height: 46px;
        line-height: 46px;
        text-align: center;
        background: rgba(26, 48, 222, 1);
        color: rgba(255, 255, 255, 1);
        font-size: 16px;
        border: 1px solid #1a30de;
        border-radius: 4px;
        cursor: pointer;
      }
    }
  }
}
/deep/.el-form-item {
  position: relative;
}
/deep/.el-form-item::before {
  content: "*";
  font-size: 16px;
  color: #f56c6c;
  margin-right: 4px;
  position: absolute;
  left: -15px;
  top: 12px;
}
</style>